package com.qdxwx.manage.controller.subject;

import com.qdxwx.common.exception.BusinessException;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.book.BookQuery;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.subject.Subject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 专业Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/subject")
public class SubjectController extends BaseController {

    @Autowired
    private SubjectService subjectService;
    @Autowired
    private BookService bookService;
    @Autowired
    private CourseService courseService;

    @Permissions("subject:list")
    @GetMapping("/list.html")
    public String list() {
        return "subject/subject-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        List<Subject> list = subjectService.getList();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(list.size());
        return rspData;
    }

    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        Subject subject = subjectService.get(id);
        if (subject == null) {
            subject = new Subject();
        }
        mmap.put("subject", subject);
        return "subject/subject-edit";
    }

    @PostMapping("/save.json")
    @Permissions("book:edit")
    @Log(title = "编辑专业", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if (manager == null) return error("未登录，禁止操作");
        int subjectId = TKConvert.toInteger(request.getParameter("subjectId"));
        Subject subject = new Subject();
        if (subjectId > 0) {
            subject = subjectService.get(subjectId);
            if (subject == null) return error("找不到subjectId为:" + subjectId + "的专业");
        }
        String subjectName = request.getParameter("subjectName");
        if (TKString.isEmpty(subjectName)) return error("专业名不能为空");
        subject.setSubjectName(subjectName);
        subject.setSort(TKConvert.toInteger(request.getParameter("sort")));
        int row;
        if (subjectId > 0) {
            row = subjectService.update(subject);
        } else {
            row = subjectService.insert(subject);
        }
        return toAjax(row);
    }

    @Permissions("subject:remove")
    @Log(title = "删除专业", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if (id < 1) return error("缺少id参数");
        try {
            BookQuery book = new BookQuery();
            book.setSubjectId(id);
            int bookCount = bookService.count(book);
            CourseQuery course = new CourseQuery();
            course.setSubjectId(id);
            int courseCount = courseService.count(course);
            if (bookCount > 1 || courseCount > 1) {
                return error("专业已分配，不得删除");
            }
            return toAjax(subjectService.delete(id));
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }
}
